import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:syn_4s/core/extension/build_context_extension.dart';
import 'package:syn_4s/core/extension/int_extension.dart';
import 'package:syn_4s/core/router/router.dart';
import 'package:syn_4s/core/utils/toast_utils.dart';
import 'package:syn_4s/ui/widgets/check_update_dialog.dart';

import '../../../core/viewmodel/setting_view_model.dart';
import '../../shared/app_theme.dart';
import '../../widgets/shadow_container.dart';

class SettingContent extends StatelessWidget {
  SettingContent({super.key});

  final SettingViewModel viewModel = Get.find<SettingViewModel>();

  @override
  Widget build(BuildContext context) {
    BoxDecoration decoration = getDecoration();
    return Container(
      height: double.infinity,
      decoration: const BoxDecoration(
        gradient: LinearGradient(
            colors: [AppTheme.bgStartColor, AppTheme.bgEndColor]),
      ),
      child: Column(
        children: [
          ShadowContainer(
            border: Border.all(color: AppTheme.orangeColor),
            margin: EdgeInsets.symmetric(horizontal: 12.px, vertical: 8.px),
            padding: EdgeInsets.symmetric(horizontal: 8.px, vertical: 10.px),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 8.px),
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.all(Radius.circular(4.px))),
                  child: Column(
                    children: [
                      buildContent(context.l10n.change_pwd,
                          decoration: decoration, onTap: () {
                        Get.toNamed(MyRouter.verificationPwdRoute);
                      }),
                      buildContent(context.l10n.language,
                          content: viewModel.language,
                          decoration: decoration, onTap: () {
                        Get.toNamed(MyRouter.languageRoute);
                      }),
                      buildContent(context.l10n.check_update,
                          content: viewModel.version, decoration: decoration, onTap: (){
                            if(Platform.isAndroid) {
                              viewModel.getAndroidVersion(context);
                            }
                          }),
                      buildContent(context.l10n.about_us, onTap: () {
                        Get.toNamed(MyRouter.aboutUsRoute);
                      })
                    ],
                  ),
                ),
                SizedBox(
                  height: 190.px,
                ),
                buildButton(context)
              ],
            ),
          ),
          const Spacer()
        ],
      ),
    );
  }

  ///警报
  void showAlertDialog(BuildContext context) {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) => CheckUpdateDialog(),
    );
  }

  BoxDecoration getDecoration() {
    return const BoxDecoration(
        border: Border(bottom: BorderSide(color: AppTheme.borderColor)));
  }

  Widget buildContent(String title,
      {RxString? content, Function()? onTap, Decoration? decoration}) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        decoration: decoration,
        // padding: EdgeInsets.only(top: 6.px,bottom: 6.px),
        height: 40.px,
        alignment: Alignment.centerLeft,
        child: Row(
          children: [
            Expanded(
                child: Text(
              title,
              style: const TextStyle(color: Colors.black),
            )),
            content != null
                ? Obx(() => Text(
                      content.value,
                      style: const TextStyle(color: AppTheme.sixColor),
                    ))
                : const SizedBox.shrink(),
            const Icon(Icons.chevron_right, color: AppTheme.sixColor)
          ],
        ),
      ),
    );
  }

  Widget buildButton(BuildContext context) {
    return Container(
      width: double.infinity,
      height: 44.px,
      child: ElevatedButton(
        onPressed: () {
          viewModel.logout(context);
        },
        style: ElevatedButton.styleFrom(
          shape: RoundedRectangleBorder(
            side: const BorderSide(color: AppTheme.orangeColor),
            borderRadius: BorderRadius.circular(24.px),
          ),
        ),
        child: Text(
          context.l10n.logout,
          style: TextStyle(color: AppTheme.orangeColor, fontSize: 20.px),
        ),
      ),
    );
  }
}
